<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('用户数据库')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>别名：</label>
                            <input type="text" name="alias"/>
                        </li>
                        <li>
                            <label>库名称：</label>
                            <input type="text" name="databaseName"/>
                        </li>
                        <li>
                            <label>库状态：</label>
                            <select name="databaseStatus" th:with="type=${@dict.getType('database_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>状态：</label>
                            <select name="status" th:with="type=${@dict.getType('database_user_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="datamonitor:myUserDatabase:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="datamonitor:myUserDatabase:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" id="deleteMyDatabase" onclick="removeAll()"
               shiro:hasPermission="datamonitor:myUserDatabase:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()"
               shiro:hasPermission="datamonitor:myUserDatabase:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('datamonitor:myUserDatabase:edit')}]];
    var removeFlag = [[${@permission.hasPermi('datamonitor:myUserDatabase:remove')}]];
    var databaseTypeDatas = [[${@dict.getType('database_type')}]];
    var databaseUserStatusDatas = [[${@dict.getType('database_user_status')}]];
    var databaseStatusDatas = [[${@dict.getType('database_status')}]];
    var prefix = ctx + "datamonitor/myUserDatabase";
    var checkMap = new Map()

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "我的数据库信息",
            columns: [{
                checkbox: true
            },
                {
                    field: 'userDatabaseId',
                    title: '',
                    visible: false
                },
                {
                    field: 'alias',
                    title: '别名'
                },
                {
                    field: 'databaseName',
                    title: '库名称'
                },
                {
                    field: 'databaseType',
                    title: '类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(databaseTypeDatas, value);
                    }
                },
                {
                    field: 'databaseHost',
                    title: '主机'
                },
                {
                    field: 'databasePort',
                    title: '端口'
                },
                {
                    field: 'databaseDbname',
                    title: '库'
                },
                {
                    field: 'databaseUser',
                    title: '库用户名'
                },
                {
                    field: 'createBy',
                    title: '创建者'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    field: 'databaseStatus',
                    title: '库状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(databaseStatusDatas, value);
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(databaseUserStatusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.userDatabaseId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        if (row.status === '0') {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="apply(\'' + row.userDatabaseId + '\')"><i class="fa fa-edit"></i>申请</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.userDatabaseId + '\')"><i class="fa fa-remove"></i>删除</a>');

                        }
                        return actions.join('');
                    }
                }],
            onCheck: onCheck,
            onUncheck: onUncheck,
            onCheckAll: onCheckAll,
            onUncheckAll: onUncheckAll
        };
        $.table.init(options);
    });

    function apply(userDatabaseId) {
        var param = {"userDatabaseId": userDatabaseId, "status": '1'}
        $.operate.save(prefix + "/edit", tansParams(param));
    }


    function onCheck(row, $element) {
        checkMap.set(row.databaseId, row);
    }

    function onUncheck(row, $element) {
        checkMap.delete(row.databaseId);
    }

    function onCheckAll(rowsAfter, rowsBefore) {
        var rows = $.map(rowsAfter, function (row) {
            checkMap.set(row.databaseId, row)
        });
    }

    function onUncheckAll(rowsAfter, rowsBefore) {
        checkMap = new Map();
    }

    function removeAll() {
        for (let [key, row] of checkMap) {
            if (row.status != '0' && row.databaseStatus != '-1') {
                $.modal.alertError('数据库名称为:' + row.databaseName + '无法删除！,只能删除草稿或者已删除状态的库')
                return;
            }
        }
        $.operate.removeAll();
    }


    function checkDatabaseStatus() {
        for (let [key, row] of checkMap) {
            if (row.status != '0') {
                return false;
            }
        }
        return true;
    }

</script>
</body>
</html>